package com.hnkywl.sts.service.xsgl;

import com.google.gson.GsonBuilder;
import com.hnkywl.sts.dao.organ.NbgsDao;
import com.hnkywl.sts.dao.xsgl.KcbjDao;
import com.hnkywl.sts.entity.organ.Nbgs;
import com.hnkywl.sts.entity.xsgl.Kcbj;
import com.hnkywl.sts.service.sys.YgService;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-4-22
 * Time: 下午3:38
 * To change this template use File | Settings | File Templates.
 */
@Service
public class KcbjService {
    private KcbjDao kcbjDao;
    private NbgsDao nbgsDao;

    /**
     * 查询库存报价列表
     *
     * @param bjrq
     * @param bjgsId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Transactional
    public String findAllKcbj(Date bjrq, Long bjgsId, List bjrqList, int pageNo, int pageSize) {
        Page<Map> kcbjPage = kcbjDao.findKcbjList(bjrq, bjgsId,bjrqList, pageNo, pageSize);
        List<Map> kcbjList = kcbjPage.getResult() == null ? new ArrayList<Map>() : kcbjPage.getResult();
        return "{\"total\":" + kcbjPage.getTotalCount() + ",\"rows\":" + new GsonBuilder().setDateFormat("yyyy-MM-dd").create().toJson(kcbjList) + "}";
    }

    public String findKcbjList(Date bjrq, Long bjgsId, int pageNo, int pageSize) {
        Page<Map> kcbjPage = kcbjDao.findAllKcbj(bjrq, bjgsId, pageNo, pageSize);
        List<Map> kcbjList = kcbjPage.getResult() == null ? new ArrayList<Map>() : kcbjPage.getResult();
        return "{\"total\":" + kcbjPage.getTotalCount() + ",\"rows\":" + new GsonBuilder().setDateFormat("yyyy-MM-dd").create().toJson(kcbjList) + "}";
    }
    //库存报价审批列表
    public String findKcbjListSp(Date bjrq, Long bjgsId, int pageNo, int pageSize) {
        Page<Map> kcbjPage = kcbjDao.findAllKcbjSp(bjrq, bjgsId, pageNo, pageSize);
        List<Map> kcbjList = kcbjPage.getResult() == null ? new ArrayList<Map>() : kcbjPage.getResult();
        return "{\"total\":" + kcbjPage.getTotalCount() + ",\"rows\":" + new GsonBuilder().setDateFormat("yyyy-MM-dd").create().toJson(kcbjList) + "}";
    }

    @Transactional
    public List<Map> findAllKcbj(Date bjrq, Long bjgsId, List bjrqList) {
        Page<Map> kcbjPage = kcbjDao.findKcbjList(bjrq, bjgsId,bjrqList, 1, -1);
        List<Map> kcbjList = kcbjPage.getResult() == null ? new ArrayList<Map>() : kcbjPage.getResult();
        return kcbjList;
    }

    @Transactional
    public List<Kcbj> findKcbj(Date bjrq, Long bjgsId) {
        return kcbjDao.findKcbj(bjrq, bjgsId);
    }

    /**
     * 保存库存报价信息
     *
     * @param kcbjList 库存报价实体
     * @return
     */
    @Transactional
    public void saveKcbj(List<Kcbj> kcbjList, Long bjgsId) {
        if (kcbjList != null && kcbjList.size() != 0)
            kcbjDao.delete(kcbjList.get(0).getBjrq(), bjgsId);
        for (Kcbj kcbj : kcbjList) {
            if (!(kcbj.getBj() == null && kcbj.getScbj() == null && kcbj.getWj() == null)) {
                kcbj.setCreateUser(YgService.getCurrentYg());
                kcbj.setCreateOrgan(YgService.getCurrentYg().getZggs());
                kcbj.setCreateDepartment(YgService.getCurrentYg().getZgjg());
                kcbj.setDateCreated(new Date());
                kcbj.setLastUpdated(kcbj.getDateCreated());
                kcbjDao.save(kcbj);
            }
        }
    }

    /**
     * 保存库存报价信息
     *
     * @param kcbj 库存报价实体
     * @return
     */
    @Transactional
    public void saveKcbj(Kcbj kcbj) {
        kcbjDao.save(kcbj);
    }

    /**
     * 保存核定库存报价
     *
     * @param kcbjList
     */
    @Transactional
    public void saveHdkcbj(List<Kcbj> kcbjList) {
        for (Kcbj kcbj : kcbjList) {
            if (!(kcbj.getHdbj() == null && kcbj.getHdscbj() == null && kcbj.getHdwj() == null)) {
                Kcbj k = kcbjDao.get(kcbj.getId());
                k.setLastUpdated(new Date());
                k.setHdbj(kcbj.getHdbj());
                k.setHdscbj(kcbj.getHdscbj());
                k.setHdwj(kcbj.getHdwj());
                kcbjDao.save(k);
            }
        }
    }

    /**
     * 查询库存报价明细
     *
     * @param id
     * @return
     */
    @Transactional
    public Kcbj findKcbjDetail(Long id) {
        return this.kcbjDao.get(id);
    }

    @Autowired
    public void setKcbjDao(KcbjDao kcbjDao) {
        this.kcbjDao = kcbjDao;
    }

    @Autowired
    public void setNbgsDao(NbgsDao nbgsDao) {
        this.nbgsDao = nbgsDao;
    }
}
